import axios from 'axios'
import {
  Toast
} from 'vant'
import JSONbig from 'json-bigint';
const baseURL = process.env.VUE_APP_BASE_URL
const instance = axios.create({
  baseURL,
  timeout: 15000,
  transformResponse: [
    function(data) {
      if (typeof data === 'string') {
        const JSONbigString = new JSONbig({
          storeAsString: true
        });
        return JSONbigString.parse(data);
      } else {
        return data;
      }
    }
  ]
});

// request interceptors
instance.interceptors.request.use((config, promise) => {
  return config
  // }
}, (error) => {
  return Promise.reject(error)
})

instance.interceptors.response.use(function(response) {
  if (response.data.success) {
    return response.data.data;
  } else {
    Toast(response.data.errorMessage);
    return Promise.reject(response.data.errorMessage)
  }
}, (error) => {
  if (error.response.status === 500) {
    Toast("网络错误")
  }
  return Promise.reject(error)
});

export default instance
